package com.css.fxfzcpxx.modules.houseSeismicEvaluation.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzcpxx.base.response.ResponseCode;
import com.css.fxfzcpxx.modules.houseSeismicEvaluation.bo.GenerateVulnerabilityMatrixVo;
import com.css.fxfzcpxx.modules.houseSeismicEvaluation.entity.BuildingStructureParam;
import com.css.fxfzcpxx.modules.houseSeismicEvaluation.entity.EstResult;
import com.css.fxfzcpxx.modules.houseSeismicEvaluation.entity.HouSeiEvaluation;
import com.css.fxfzcpxx.modules.houseSeismicEvaluation.repository.EstResultRepository;
import com.css.fxfzcpxx.modules.houseSeismicEvaluation.repository.HouSeiEvaluationRepository;
import com.css.fxfzcpxx.modules.houseSeismicEvaluation.service.HouSeiEvaluationService;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import com.css.fxfzcpxx.util.UUIDGenerator;
import com.css.fxfzcpxx.web.PlatformSessionContext;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class HouSeiEvaluationServiceImpl implements HouSeiEvaluationService {
    @Resource
    HouSeiEvaluationRepository houSeiEvaluationRepository;

    @Resource
    EstResultRepository estResultRepository;

    @Autowired
    private RestTemplate restTemplate;



    @Override
    public HashMap<String, List<GenerateVulnerabilityMatrixVo>> getData(BuildingStructureParam param) throws Exception {

        //设置超时时间
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(0);
        clientHttpRequestFactory.setReadTimeout(0);

        restTemplate.setRequestFactory(clientHttpRequestFactory);

        HashMap<String, List<GenerateVulnerabilityMatrixVo>> resultMap=new HashMap<>();
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("ysxRestUrl") + "/customVulnerability/dataMatching";
        JSONObject valueMap = new JSONObject();
        valueMap.put("province", param.getProvince());
        valueMap.put("city", param.getCity());
        valueMap.put("county", param.getCounty());
        valueMap.put("structureType", param.getStructureType());
        valueMap.put("structureType1", param.getStructureType1());
        valueMap.put("fortification", param.getFortification());
        valueMap.put("years", param.getYears());
        valueMap.put("use0", param.getUse0());
        valueMap.put("use1", param.getUse1());
        valueMap.put("cs", param.getCs());
        valueMap.put("yearsList", param.getYearsList());
        valueMap.put("useList", param.getUseList());
        valueMap.put("csList", param.getCsList());
        valueMap.put("type", param.getType());


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzcpxx");
        headers.set("subsystemCode", "dzxczhsspgzxt");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);
        //JSONObject data1 = restTemplate.exchange(url, HttpMethod.POST, r, JSONObject.class).getBody();
        JSONObject body = restTemplate.exchange(url, HttpMethod.POST, r, JSONObject.class).getBody();
        Integer code = (Integer) body.get("code");
        if(ResponseCode.OK==code){
            JSONObject data = body.getJSONObject("data");
            ObjectMapper objectMapper = new ObjectMapper();
            resultMap = objectMapper.readValue(data.toString(), new TypeReference<HashMap<String, List<GenerateVulnerabilityMatrixVo>>>(){});
        }else{
            String message = (String) body.get("message");
            throw new RuntimeException(message);
        }
        return resultMap;
    }

    @Transactional
    @Override
    public void save(GenerateVulnerabilityMatrixVo generateVulnerabilityMatrixVo) {

       /* HouSeiEvaluation houSeiEvaluation = generateVulnerabilityMatrixVo.getHouSeiEvaluation();
        String uuid = UUIDGenerator.getUUID();
        houSeiEvaluation.setId(uuid);
        houSeiEvaluation.setCreateUser(PlatformSessionContext.getUserID());
        houSeiEvaluation.setCreateTime(new Date());

        List<BuildingStructureParam> list = generateVulnerabilityMatrixVo.getList();
        for (BuildingStructureParam item : list) {
            EstResult estResult=new EstResult();
            estResult.setId(UUIDGenerator.getUUID());
            estResult.setChseId(uuid);
            if (!PlatformObjectUtils.isEmpty(item.getIntensity())) {
                estResult.setIntensity(item.getIntensity());
            }
            if (!PlatformObjectUtils.isEmpty(item.getJsonData())) {
                estResult.setBreakState(item.getJsonData());
            }
            estResultRepository.save(estResult);
        }

        houSeiEvaluationRepository.save(houSeiEvaluation);*/
    }
}
